﻿Imports System.Xml
Imports System.Net

Public Class Form1

    Dim _source As New List(Of NuItem)

    Enum SourceType
        Algemeen
        Sport
        Economie
        Tech
        Overig
    End Enum

    Private Sub GetItems(ByVal url As String)
        Dim xmldoc As New XmlDocument

        xmldoc.Load(url)

        For Each nod As XmlNode In xmldoc.SelectNodes("/rss/channel/item")
            Dim req As WebRequest
            Dim resp As WebResponse
            Dim result As Image

            Try
                req = WebRequest.Create(nod.SelectSingleNode("enclosure").Attributes("url").InnerText)
                resp = req.GetResponse
                result = Image.FromStream(resp.GetResponseStream)

            Catch ex As Exception

            End Try

            _source.Add(New NuItem(nod.SelectSingleNode("title").InnerText, nod.SelectSingleNode("description").InnerText, result, nod.SelectSingleNode("pubDate").InnerText, nod.SelectSingleNode("link").InnerText))
        Next

    End Sub

    Public Sub GetItems(ByVal source As SourceType)

        Select Case source
            Case SourceType.Algemeen
                GetItems("http://www.nu.nl/feeds/rss/algemeen.rss")

            Case SourceType.Economie
                GetItems("http://www.nu.nl/feeds/rss/economie.rss")

            Case SourceType.Sport
                GetItems("http://www.nu.nl/feeds/rss/sport.rss")

            Case SourceType.Tech
                GetItems("http://www.nu.nl/feeds/rss/tech.rss")

            Case SourceType.Overig
                GetItems("http://www.nu.nl/feeds/rss/overig.rss")

        End Select

    End Sub


    Private Sub lbl_title_Click(sender As Object, e As EventArgs) Handles lbl_title.Click

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False

        If worker.IsBusy = False Then
            worker.RunWorkerAsync()
        End If
    End Sub

    Private Sub Tbcontrol1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Tbcontrol1.SelectedIndexChanged
        If worker.IsBusy = False Then
            worker.RunWorkerAsync()
        End If
    End Sub

    Private Sub worker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles worker.DoWork
        Me.Text = "Laden..."

        Lbox6.Items.Clear()
        Lbox7.Items.Clear()
        Lbox3.Items.Clear()
        Lbox4.Items.Clear()
        Lbox5.Items.Clear()

        _source.Clear()

        Select Case Tbcontrol1.SelectedIndex
            Case 0
                GetItems(SourceType.Algemeen)

            Case 1
                GetItems(SourceType.Economie)

            Case 2
                GetItems(SourceType.Sport)

            Case 3
                GetItems(SourceType.Tech)

            Case 4
                GetItems(SourceType.Overig)

        End Select
    End Sub

    Private Sub worker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles worker.RunWorkerCompleted
        For Each it As NuItem In _source
            Select Case Tbcontrol1.SelectedIndex
                Case 0
                    Lbox3.Items.Add(it.Title & "| " & it.PublishDate.Replace("+0200", "") & "| " & it.Description)
                Case 1
                    Lbox4.Items.Add(it.Title & "| " & it.PublishDate.Replace("+0200", "") & "| " & it.Description)
                Case 2
                    Lbox5.Items.Add(it.Title & "| " & it.PublishDate.Replace("+0200", "") & "| " & it.Description)
                Case 3
                    Lbox6.Items.Add(it.Title & "| " & it.PublishDate.Replace("+0200", "") & "| " & it.Description)
                Case 4
                    Lbox7.Items.Add(it.Title & "| " & it.PublishDate.Replace("+0200", "") & "| " & it.Description)
            End Select
        Next

        Select Case Tbcontrol1.SelectedIndex
            Case 0
                Me.Text = "Algemeen nieuws - NU.nl"

                Lbox3.SelectedIndex = 0
            Case 1
                Me.Text = "Economisch nieuws - NU.nl"

                Lbox4.SelectedIndex = 0
            Case 2
                Me.Text = "Sport nieuws - NU.nl"

                Lbox5.SelectedIndex = 0
            Case 3
                Me.Text = "Technologisch nieuws - NU.nl"

                Lbox6.SelectedIndex = 0
            Case 4
                Me.Text = "Overig nieuws - NU.nl"

                Lbox7.SelectedIndex = 0
        End Select
    End Sub

    Private Sub Lbox6_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Lbox6.SelectedIndexChanged

        lbl_title.Text = _source(Lbox6.SelectedIndex).Title
        lbl_description.Text = _source(Lbox6.SelectedIndex).Description
        pic_thumb.Image = _source(Lbox6.SelectedIndex).Thumbnail

        Me.Invalidate()
    End Sub

    Private Sub Lbox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Lbox3.SelectedIndexChanged

        lbl_title.Text = _source(Lbox3.SelectedIndex).Title
        lbl_description.Text = _source(Lbox3.SelectedIndex).Description
        pic_thumb.Image = _source(Lbox3.SelectedIndex).Thumbnail

        Me.Invalidate()
    End Sub

    Private Sub Lbox6_SelectedValueChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub Lbox6_MouseClick(sender As Object, e As MouseEventArgs)

    End Sub

    Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Try
            Process.Start("http://www.nu.nl")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
        If worker.IsBusy = False Then
            worker.RunWorkerAsync()
        End If
    End Sub

    Private Sub Lbox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Lbox4.SelectedIndexChanged
        lbl_title.Text = _source(Lbox4.SelectedIndex).Title
        lbl_description.Text = _source(Lbox4.SelectedIndex).Description
        pic_thumb.Image = _source(Lbox4.SelectedIndex).Thumbnail

        Me.Invalidate()
    End Sub

    Private Sub Lbox5_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Lbox5.SelectedIndexChanged
        lbl_title.Text = _source(Lbox5.SelectedIndex).Title
        lbl_description.Text = _source(Lbox5.SelectedIndex).Description
        pic_thumb.Image = _source(Lbox5.SelectedIndex).Thumbnail

        Me.Invalidate()
    End Sub

    Private Sub Lbox7_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Lbox7.SelectedIndexChanged
        lbl_title.Text = _source(Lbox7.SelectedIndex).Title
        lbl_description.Text = _source(Lbox7.SelectedIndex).Description
        pic_thumb.Image = _source(Lbox7.SelectedIndex).Thumbnail

        Me.Invalidate()
    End Sub
End Class

Public Class NuItem

    Dim _title As String
    Public ReadOnly Property Title As String
        Get
            Return _title
        End Get
    End Property

    Dim _description As String
    Public ReadOnly Property Description As String
        Get
            Return _description
        End Get
    End Property

    Dim _thumbnail As Image
    Public ReadOnly Property Thumbnail As Image
        Get
            Return _thumbnail
        End Get
    End Property

    Dim _published As String
    Public ReadOnly Property PublishDate As String
        Get
            Return _published
        End Get
    End Property

    Dim _guid As String
    Public ReadOnly Property Guid As Uri
        Get
            Return New Uri(_guid)
        End Get
    End Property

    Public Sub New(ByVal title As String, ByVal description As String, ByVal thumb As Image, ByVal publishdate As String, ByVal guid As String)
        _title = title
        _description = description
        _thumbnail = thumb
        _published = publishdate
        _guid = guid
    End Sub

End Class